package com.huangjue.backend.utils;


import com.huangjue.backend.constant.ResponseFormats;
import com.huangjue.backend.constant.RoleTypes;
import com.huangjue.backend.dto.aiChatClient.AiRequestMessage;
import com.huangjue.backend.dto.aiChatClient.ClientRequest;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

public final class ClientRequestBuilder {

    private String model = "kimi-k2-0711-preview";
    private final List<AiRequestMessage> messages = new ArrayList<>();
    private double temperature = 0.5;
    private boolean stream = false;
    private String responseFormat = "text";

    /* ---------- fluent API ---------- */

    public ClientRequestBuilder model(String model) {
        this.model = Objects.requireNonNull(model);
        return this;
    }

    public ClientRequestBuilder system(String prompt) {
        // 同时校验 null 和空字符串
        if (prompt == null || prompt.trim().isEmpty()) {
            throw new IllegalArgumentException("系统提示词不能为空或空白");
        }
        return add(RoleTypes.SYSTEM, prompt);
    }

    public ClientRequestBuilder user(String text) {
        if (text == null || text.trim().isEmpty()) {
            throw new IllegalArgumentException("用户输入不能为空或空白");
        }
        return add(RoleTypes.USER, text);
    }

    public ClientRequestBuilder assistant(String text) {
        if (text == null || text.trim().isEmpty()) {
            throw new IllegalArgumentException("Ai的历史回答不能为空或空白");
        }
        return add(RoleTypes.ASSISTANT, text);
    }

    public ClientRequestBuilder add(String role, String content) {
        // 参数非空校验
        Objects.requireNonNull(role, "Role cannot be null");
        Objects.requireNonNull(content, "Content cannot be null");
        messages.add(new AiRequestMessage(role, content));
        return this;
    }

    public ClientRequestBuilder addAll(Collection<AiRequestMessage> history) {
        messages.addAll(history);
        return this;
    }

    public ClientRequestBuilder temperature(double t) {
        this.temperature = t;
        return this;
    }

    public ClientRequestBuilder stream(boolean s) {
        this.stream = s;
        return this;
    }

    public ClientRequestBuilder responseFormat(String responseFormat) {
        if ("text".equals(responseFormat) || "json_object".equals(responseFormat)) {
            this.responseFormat = responseFormat;
        } else {
            this.responseFormat = ResponseFormats.TEXT;
        }
        return this;
    }

    /* ---------- build ---------- */

    public ClientRequest build() {
        if (messages.isEmpty()) {
            throw new IllegalStateException("messages 不能为空");
        }
        return ClientRequest.builder()
                .model(model)
                .messages(List.copyOf(messages))
                .temperature(temperature)
                .stream(stream)
                .responseFormat(ClientRequest.ResponseFormat.of(responseFormat))
                .build();
    }

}
